package com.buttonpay.favourite;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.buttonpay.DataHelper;
import com.buttonpay.ImageLoader;
import com.buttonpay.Products;
import com.buttonpay.R;
import com.buttonpay.home.TopUpDialog;

public class FavouriteTab extends Activity 
{
	/** Called when the activity is first created. */
	
	ArrayList<Products> topUpFav,electricityFav,digitalTVProduct,dataTopUpProduct;
	ArrayList<Integer> favouriteListicon;
	Button btnLogout,btnBack;
	ListView lvperatorList;
	TextView tvFavouriteMessage,tvHeaderName;
	private DataHelper dh;
	private SeparatedListAdapter seprateListAdapter;
	
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.favourite_list);
		
		btnLogout=(Button)findViewById(R.id.btnLogout);
		btnBack=(Button)findViewById(R.id.btnPrevious);
		btnBack.setVisibility(Button.INVISIBLE);
		
		//getting topup Products.
		topUpFav=getFavourites("2");
		favouriteListicon=new ArrayList<Integer>(); 
		//getting electricity product
		electricityFav=getFavourites("4");
		
		//getting digitalTv Product
		digitalTVProduct=getFavourites("8");
		
		//getting datatopup product
		dataTopUpProduct=getFavourites("16");
		
		lvperatorList=(ListView)findViewById(R.id.lvFavouriteList);
		tvFavouriteMessage=(TextView)findViewById(R.id.tvNofavouriteMessage);
		tvHeaderName=(TextView)findViewById(R.id.tv_headerText);
		tvHeaderName.setText("Favourite");
		 
		seprateListAdapter = new SeparatedListAdapter(this);
		
		if(topUpFav.isEmpty()&&electricityFav.isEmpty()&&digitalTVProduct.isEmpty()&&dataTopUpProduct.isEmpty())
		{
			tvFavouriteMessage.setVisibility(TextView.VISIBLE);
		}
		else 
		{
			if(!topUpFav.isEmpty())
			{
				MyListAdapter adapter = new MyListAdapter(this,topUpFav,"topup");
				seprateListAdapter.addSection("Airtime TopUp",adapter);
			}
			if(!electricityFav.isEmpty())
			{
				MyListAdapter adapter = new MyListAdapter(this,electricityFav,"electricity");
				seprateListAdapter.addSection(electricityFav.get(0).getSystemServiceName(),adapter);
			}
			if(!digitalTVProduct.isEmpty())
			{
				MyListAdapter adapter = new MyListAdapter(this,digitalTVProduct,"digitaltv");
				seprateListAdapter.addSection(digitalTVProduct.get(0).getSystemServiceName(),adapter);
			}
			if(!dataTopUpProduct.isEmpty())
			{
				MyListAdapter adapter = new MyListAdapter(this,dataTopUpProduct,"datatopup");
				seprateListAdapter.addSection(dataTopUpProduct.get(0).getSystemServiceName(),adapter);
			}
			lvperatorList.setAdapter(seprateListAdapter);
		}
		
//		if(!set2.isEmpty())
//		{
//			while(i2.hasNext()) 
//			{ 
//				
//				Map.Entry me = (Map.Entry)i2.next(); 
//				System.out.print(me.getKey() + ": "); 
//				System.out.println(me.getValue());
//				utilityFavouriteNames.add((String) me.getKey());
//				utilityFavouriteIcon.add((Integer) me.getValue());
//				
//			}
//			adapter2=new MyListAdapter(this, utilityFavouriteNames,utilityFavouriteIcon,"utility");
//		    seprateListAdapter.addSection("Utility Pay", adapter2);
//		}
//		if(!topUpFav.isEmpty())
//		{
//		
//			lvperatorList.setAdapter(seprateListAdapter);	
//		}
		
		
		btnLogout.setOnClickListener(new OnClickListener()
		{
			
			public void onClick(View v) 
			{
				// TODO Auto-generated method stub
				new AlertDialog.Builder(FavouriteTab.this)
		        .setTitle( "Exit Application" )
		        .setMessage( "Are you sure you want to Logout?" )
		        .setPositiveButton("YES", new android.content.DialogInterface.OnClickListener() 
		        {
		            public void onClick(DialogInterface arg0, int arg1) 
		            {
		                //do stuff onclick of YES
		            	setResult(2);
		            	finish();
		            }

					public void onClick(View v) 
					{
						// TODO Auto-generated method stub
						
					}
		        })
		        .setNegativeButton("NO",  new android.content.DialogInterface.OnClickListener() {
		            public void onClick(DialogInterface arg0, int arg1)
		            {
		                //do stuff onclick of CANCEL
		            	arg0.dismiss();
		               
		            }

					public void onClick(View v) 
					{
						// TODO Auto-generated method stub
					}
		        }).show();
				
			}
		});
		
		
		
		lvperatorList.setOnItemClickListener(new OnItemClickListener()
		{

			public void onItemClick(AdapterView<?> adapterView, View view, int position,
					long id) 
			{
			 
				Products p=(Products)adapterView.getAdapter().getItem(position);
				System.out.println("Name:"+p.getProductName()+"\nBach ID:"+p.getBatchID()+"\n Product Type"+p.getProductType());
				
				TopUpDialog topUpDialog=new TopUpDialog(view.getContext(),p);
				topUpDialog.show();
			}
			
		});
	}
	 
	@Override
	public void onBackPressed() 
	{
		// TODO Auto-generated method stub
		new AlertDialog.Builder(FavouriteTab.this)
        .setTitle( "Exit Application." )
        .setMessage( "Are you sure you want to Exit ?" )
        .setPositiveButton("YES", new android.content.DialogInterface.OnClickListener() 
        {
            public void onClick(DialogInterface arg0, int arg1) 
            {
                //do stuff onclick of YES
            	finish();
            }

			public void onClick(View v) 
			{
				// TODO Auto-generated method stub
				
			}
        })
        .setNegativeButton("NO",  new android.content.DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1)
            {
                //do stuff onclick of CANCEL
            	arg0.dismiss();
               
            }

			public void onClick(View v) 
			{
				// TODO Auto-generated method stub
			}
        }).show();
	}
	
	private ArrayList<Products> getFavourites(String sysSerID) 
	{

		try
		{
			dh=new DataHelper(FavouriteTab.this);
			return dh.getFavTopUpProducts(sysSerID);
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return null;
		}
		finally
		{
			if(dh!=null)
				dh.close();
		}
		
	}


	//---------------------
	 public static class ViewHolder
	 {
			public TextView tvOperatorName;
			public ImageView ivOperatorIcon; 
		 
     }
	
	 class MyListAdapter extends ArrayAdapter<Products> 
	 {	
			
		private final Context context;
		ArrayList<Products> products;
		ImageLoader imageLoader; 
			
		String sectionName;
		LayoutInflater vi ;			  
		
		public MyListAdapter(Context context, ArrayList<Products> products, String sectionName)
		{
			super(context, R.layout.row_favourite_list, products);
			this.context = context;
			this.products=products;
			this.imageLoader=new ImageLoader(context);
			this.sectionName=sectionName;
			vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				
		}
 
		 
			public View getView(final int position, View convertView, ViewGroup parent) 
			{
				View v = convertView;
				ViewHolder holder = null;
				
				if (v == null) 
				{
					holder=new ViewHolder();  
					v = vi.inflate(R.layout.row_favourite_list, null); 
					holder.tvOperatorName=(TextView) v.findViewById(R.id.tvOperatorName);
					holder.ivOperatorIcon=(ImageView)v.findViewById(R.id.ivOperatorIcon);
					v.setTag(holder);		
				}
				else
					holder=(ViewHolder)v.getTag();
				
				
					System.out.println("Product Name:"+this.products.get(position).getProductName());
					System.out.println("Image URl:"+this.products.get(position).getImageURL());
					
					holder.tvOperatorName.setText(this.products.get(position).getProductDesc());
					holder.tvOperatorName.setTag(sectionName);
					
					holder.ivOperatorIcon.setTag(this.products.get(position).getImageURL());
					imageLoader.DisplayImage(this.products.get(position).getImageURL(), FavouriteTab.this,holder.ivOperatorIcon);
					//holder.ivOperatorIcon.setImageResource(this.products.get(position).getImageURL()));
					
					return v;
			}
			
		}
	
}